//
//  ALPS_TListNLDViewController.m
//  Alps
//
//  Created by dllo on 16/6/11.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_TListNLDViewController.h"
#import "WSSNetTool.h"
#import "ALPS_TListDModel.h"
#import "ALPS_TListDTCell.h"
#import "ALPS_ClassifyClickClickAlbumController.h"
@interface ALPS_TListNLDViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;

//加载页
@property (nonatomic, assign) NSInteger page;
@end

@implementation ALPS_TListNLDViewController

- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    self.page = 1;
    [self getView];
    [self getLeftdata:self.webKey type:self.typeJudgment number:1];
    [self setLeftBackButton];
    self.navigationItem.title = self.navTitle;
}

-(void)getView
{

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewStylePlain;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"TListDTCell"];
    [self.tableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"TListDTCell1"];
    [self.tableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"TListDTCell2"];
    [self.tableView registerClass:[ALPS_TListDTCell class] forCellReuseIdentifier:@"TListDTCell3"];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshData];
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadingData];
    }];
    
    
}


-(void)refreshData
{
    [self.dataSource removeAllObjects];
    [self.tableView reloadData];
    [self getLeftdata:self.webKey type:self.typeJudgment number:1];
    self.page = 1;

}


-(void)loadingData
{
    self.page++;
    [self getLeftdata:self.webKey type:self.typeJudgment number:self.page];

}



-(void)getLeftdata:(NSString *)string type:(NSString *)type number:(NSInteger)num
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/%@?device=iPhone&key=%@&pageId=%ld&pageSize=20&scale=3", type, string, num] body:nil headers:nil response:0 success:^(id result) {
        NSArray *array = [result objectForKey:@"list"];
        for (NSDictionary *dict in array) {
            ALPS_TListDModel *model = [[ALPS_TListDModel alloc] initWithDic:dict];
            [self.dataSource addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.page == [[result objectForKey:@"maxPageId"] integerValue]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        return 100;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld", self.dataSource.count);
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_TListDModel *model = [self.dataSource objectAtIndex:indexPath.row];

    //为了让前三名变色
    ALPS_TListDTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TListDTCell"];
    cell.mdoel = [self.dataSource objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellStyleDefault;
    
    cell.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
        
    
        
        
    
        
        
    

    if ([self.typeJudgment isEqualToString:@"album"]) {
        cell.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
        cell.albumView.image = [UIImage imageNamed:@"album_tracks"];
    }
    
        switch (indexPath.row + 1) {
            case 1:
            {
                ALPS_TListDTCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"TListDTCell1"];
                cell1.mdoel = [self.dataSource objectAtIndex:indexPath.row];
                cell1.selectionStyle = UITableViewCellStyleDefault;
                
                cell1.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
                cell1.rankLabel.textColor = [UIColor redColor];
                if ([self.typeJudgment isEqualToString:@"album"]) {
                    cell1.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
                    cell1.albumView.image = [UIImage imageNamed:@"album_tracks"];
                }

                return cell1;
            }
                break;
            case 2:
            {
                ALPS_TListDTCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"TListDTCell2"];
                cell2.mdoel = [self.dataSource objectAtIndex:indexPath.row];
                cell2.selectionStyle = UITableViewCellStyleDefault;
                
                cell2.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
                cell2.rankLabel.textColor = [UIColor orangeColor];
                if ([self.typeJudgment isEqualToString:@"album"]) {
                    cell2.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
                    cell2.albumView.image = [UIImage imageNamed:@"album_tracks"];
                }

                return cell2;
            }
                break;
            case 3:
            {
                ALPS_TListDTCell *cell3 = [tableView dequeueReusableCellWithIdentifier:@"TListDTCell3"];
                cell3.mdoel = [self.dataSource objectAtIndex:indexPath.row];
                cell3.selectionStyle = UITableViewCellStyleDefault;
                
                cell3.rankLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
                cell3.rankLabel.textColor = [UIColor colorWithRed:0.42 green:0.69 blue:0.22 alpha:1.00];
                if ([self.typeJudgment isEqualToString:@"album"]) {
                    cell3.bluesLabel.text = [NSString stringWithFormat:@"%ld集", model.tracksCounts];
                    cell3.albumView.image = [UIImage imageNamed:@"album_tracks"];
                }

                return cell3;
            }
                break;
            default:
                
                break;
        }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.typeJudgment isEqualToString:@"album"]) {
        ALPS_ClassifyClickClickAlbumController *ciyVC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
        ALPS_TListDModel *dataModel = [self.dataSource objectAtIndex:indexPath.row];        
        ciyVC.albumId = dataModel.albumId;
        [self.navigationController pushViewController:ciyVC animated:YES];
    }else{
        ALPS_PlayerMainViewController *pmVC = [[ALPS_PlayerMainViewController alloc] init];
        ALPS_TListDModel *dataModel = [self.dataSource objectAtIndex:indexPath.row];
        pmVC.trackId = dataModel.trackId;
        pmVC.listArr = self.dataSource;
        [self.navigationController pushViewController:pmVC animated:YES];
    
    }


}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
